<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/3
 * Time: 18:24
 */

namespace app\api\controller\v1;

use app\api\model\Payment as PaymentModel;
use think\Exception;
use think\Log;
use think\Request;
use think\Session;
use app\api\controller\BaseController;

class Payment extends BaseController
{
    /**
     * 添加
     */
    public function add()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			$all['mid']= $this->getLoginId();
			if(empty($all['mid'])){
				return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
			}
			
			if(empty($all['ifdefault'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['paytype'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['name'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['addr'])){
				$all['addr']="";
			}elseif(empty($all['num'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			
			$data = PaymentModel::addOnePayment($all);
			
			$datas = $data->toArray();
			$all['id'] = $datas['id'];
			if($all['ifdefault'] == 1){
				$dataone = PaymentModel::selectOnePaymentByIdMid($all);
				if($dataone){
					PaymentModel::updOnePaymentDef($all);
				}
			}
			
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**
     * 修改
     */
    public function update()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			$all['mid']= $this->getLoginId();
			if(empty($all['mid'])){
				return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
			}
			
			if(empty($all['id'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['ifdefault'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['paytype'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['name'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}elseif(empty($all['addr'])){
				$all['addr']="";
			}elseif(empty($all['num'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			
			$dataone = PaymentModel::selectOnePaymentByID($all['id']);
			if($dataone){
				
				if($all['ifdefault'] == 1){
					$dataone = PaymentModel::selectOnePaymentByIdMid($all);
					if($dataone){
						PaymentModel::updOnePaymentDef($all);
					}
				}
				
				$data = PaymentModel::updOnePayment($all);
				if($data){
					return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
				}else{
					return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
				}
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
			
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**
     * 列表
     */
    public function lists()
    {
        $mid = $this->getLoginId();
		if(empty($mid)){
			return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
		}
		
		$data = PaymentModel::paymentListsByMid($mid);
		if($data){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
		}
    }
	
	/**
     * 删除
     */
    public function del($id)
    {
        $mid = $this->getLoginId();
		if(empty($mid)){
			return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
		}
		
        
		if(empty($id)){
			return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
		}
		$dataone = PaymentModel::selectOnePaymentByID($id);
		if($dataone){
			$data = PaymentModel::delOnePayment($id);
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!"]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
			}
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
    }
	
	/**
     * 修改默认
     */
    public function updatedef()
    {
		if (request()->isPost()){
			$all = Request::instance()->param();
			
			$all['mid']= $this->getLoginId();
			if(empty($all['mid'])){
				return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
			}
			
			if(empty($all['id'])){
				return json(["returnCode"=>-6,"returnMsg"=>"post必填参数不能为空!","data"=>""]);exit;
			}
			
			$dataone = PaymentModel::selectOnePaymentByIdMid($all);
			if($dataone){
				$data = PaymentModel::updOnePaymentDef($all);
				if($data){
					return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>""]);
				}else{
					return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
				}
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
			}
			
		}else{
			return json(["returnCode"=>-6,"returnMsg"=>"post参数不能为空!","data"=>""]);
			
		}
    }
	
	/**查询一条记录*/
	public function find($id){
	    $all['mid']= $this->getLoginId();
		if(empty($all['mid'])){
			return json(["returnCode"=>-7,"returnMsg"=>"请登录!","data"=>""]);exit;
		}
			
		$data = PaymentModel::selectOnePaymentByID($id);
		if($data){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
	}
	
	
}